﻿Public Class CCANCELLabelState : Inherits CStateBase

    ''' <summary>
    ''' 実行可能かを判断する
    ''' </summary>
    ''' <param name="task"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Overrides Function IsEnter(ByVal task As CTaskBase) As Boolean
        Debug.WriteLine("CCANCELLabelState.IsEnter")
        If CCommon.isInStr(task.ActionCd, "CANCEL_LABEL") Then
            Return True
        Else
            Return False
        End If
    End Function

    ''' <summary>
    ''' 実行メソッド
    ''' </summary>
    ''' <param name="task"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Overrides Function DoExecute(ByVal task As CTaskBase) As CStateBase
        Dim nextState As CStateBase
        Dim pdaTask As CPDATask = DirectCast(task, CPDATask)
        Debug.WriteLine("CCANCELLabelState.DoExecute")
        Dim checkInfo As CCheck = CCheck.GetIns
        Dim AppManager As CApplicationManager = CApplicationManager.GetInstance

        'Dim waitTask As CPersonal = New CPersonal(pdaTask)
        Debug.WriteLine("CCANCELLabelState.DoExecute...START")
        'MessageBox.Show("INNER_BARCD:" & task.ProcessText)

        If task.ProcessText = "" Then
            '' リーダーからデータを取得する(待ち時間制限なし)
            pdaTask.WindowManager.SetMessage(CConst.CNS_MSG_021, CConst.COLOR_BLACK, CConst.COLOR_GREEN)
            CCommon.FlagManage("CANCEL_STATUS", True)
            task.ProcessText = task.ReadTextWait(-1)
            Debug.WriteLine("読んだバーコード:" & task.ProcessText)
        End If

        If Not CPersonal.CodeCheck(task.ProcessText, Me) Then
            task.ProcessText = ""
            Return Me
        End If

        Try
            ''バーコード判別
            Select Case CBarcode.GetBarcodeType(task.ProcessText)

                Case EnumBarcodeType.INNER_LABEL_CODE
                    'Unique.csvを検査
                    'なければ自分のユニークを検査->あれば自分のユニークDataで削除
                    'なければエラー
                    Dim uniqueCd As String = CBarcode.getBarcodeVl(task.ProcessText, EnumBarcodeType.INNER_LABEL_CODE, EnumBarcodeType.UNIQUE_CD)
                    Dim UniqueInfo As CCheck.UniqueCdData = checkInfo.ChkUniqueCdData(uniqueCd)

                    '過去履歴あるので取り消し可能-
                    'サーバーに送信判断
                    '   未送信の場合自分のもので送信済する
                    '   送信済の場合はサーバーに送信する
                    If Not UniqueInfo.uniqueCd Is Nothing Then
                        Dim historyOrderCd As String = checkInfo.GetLabelOrderCd(uniqueCd)

                        Dim labelArrayList As ArrayList = checkInfo.GetLabelData(historyOrderCd)
                        Dim actQty As Integer = UniqueInfo.actionQty

                        Dim delFlg As Boolean = True
                        ' If Not orderData.orderCd Is Nothing Then
                        If Not labelArrayList Is Nothing Then
                            For i As Integer = 0 To labelArrayList.Count - 1
                                Dim labelData As CCheck.LabelData = labelArrayList(i)
                                If uniqueCd = labelData.uniqueCd Then
                                    delFlg = False
                                    '自分ラベルデータから取得
                                    actQty = labelData.labelQty

                                    If labelData.isSend Then
                                        'サーバーに送信
                                        pdaTask.WindowManager.SetMessage(CConst.CNS_MSG_027, CConst.COLOR_BLACK, CConst.COLOR_GREEN)
                                        Dim uniqueArray As New ArrayList
                                        'uniqueArray.Add(uniqueCd & vbTab & pdaTask.PdaNo & vbTab & pdaTask.EmpCd)
                                        uniqueArray.Add(uniqueCd & vbTab & pdaTask.PdaNo & vbTab & pdaTask.EmpCd & vbTab & UniqueInfo.orderCd & vbTab & actQty)

                                        Dim filePath As String = CApplicationManager.GetRealPath(CApplicationManager.SETTING_PATH, CApplicationManager.EnumSpecialFolder.PATH_MYDOCUMENT)
                                        CBgControl.BgThread("CANCEL", "csv", filePath, uniqueArray, CConst.ORDER_CANCEL_UPLOAD)

                                    End If

                                    'ラベル削除->(ユニークコード削除)
                                    If checkInfo.DelLabelData(task.ProcessText) Then
                                        'MessageBox.Show("UNIQUE_DATA_DELETE")
                                        pdaTask.Hardware.nextC()
                                        '注文番号を取り消された注文番号を選択する
                                        pdaTask.OrderCd = historyOrderCd
                                        Exit For
                                    End If

                                Else

                                End If
                            Next
                        End If
                        If delFlg Then
                            'サーバーに送信
                            pdaTask.WindowManager.SetMessage(CConst.CNS_MSG_027, CConst.COLOR_BLACK, CConst.COLOR_GREEN)
                            Dim uniqueArray As New ArrayList
                            'uniqueArray.Add(uniqueCd & vbTab & pdaTask.PdaNo & vbTab & pdaTask.EmpCd)
                            uniqueArray.Add(uniqueCd & vbTab & pdaTask.PdaNo & vbTab & pdaTask.EmpCd & vbTab & UniqueInfo.orderCd & vbTab & actQty)

                            Dim filePath As String = CApplicationManager.GetRealPath(CApplicationManager.SETTING_PATH, CApplicationManager.EnumSpecialFolder.PATH_MYDOCUMENT)
                            CBgControl.BgThread("CANCEL", "csv", filePath, uniqueArray, CConst.ORDER_CANCEL_UPLOAD)

                            checkInfo.DelUniqueCdData(uniqueCd)
                            checkInfo.OrderDataCancelADD(historyOrderCd, actQty)

                            'MessageBox.Show("UNIQUE_DATA_DELETE")
                            pdaTask.Hardware.nextC()
                            '注文番号を取り消された注文番号を選択する
                            pdaTask.OrderCd = historyOrderCd

                        End If
                        'End If

                        'ユニークコード
                        Dim setData As COrderRecord = pdaTask.WindowManager.SetCurrentOrder(historyOrderCd)

                        If delFlg Then
                            '
                            setData.RemainedQty = CInt(setData.RemainedQty)
                            'setData.ActionQty -= CInt(actQty)
                            'setData.ActionBoxQty -= 1
                        Else
                            setData.RemainedQty = CInt(setData.RemainedQty) + CInt(actQty)
                            setData.ActionQty -= CInt(actQty)
                            setData.ActionBoxQty -= 1
                        End If


                        setData.IsSelectFlg = True

                        '                        CCommon.FlagManage("INNERLABEL", False)
                    Else
                        CPersonal.ErrorWindow(pdaTask, EnumWindowMode.ALERT_MODE, _
                                       Color.Black, CConst.EnumSound.ALERT_BUZZ, CConst.CNS_MSG_022, Color.White)
                    End If

                Case EnumBarcodeType.EMP_CODE
                    '交代
                    Debug.WriteLine("社員番号読みました。")

                    If Not AppManager.m_AppSetting.IsStaticReadEmpCd Then
                        '社員番号
                        '交代
                        'waitTask.Waiting(CCommon.CNS_MSG_064, pdaTask.WindowManager.GetWindowMode())
                        Dim EmpData As CCheck.EmpData = checkInfo.ChkEmpData(task.ProcessText)
                        pdaTask.WindowManager.SetEmpInfo(EmpData.empCd & ":[" & EmpData.empName & "]") '
                        pdaTask.EmpCd = EmpData.empCd
                        pdaTask.Hardware.nextD()
                    Else
                        CPersonal.ErrorWindow(pdaTask, EnumWindowMode.ALERT_MODE, _
                                            Color.Black, CConst.EnumSound.ALERT_BUZZ, CConst.CNS_MSG_020, Color.White)
                    End If
                    task.ProcessText = ""
                    task.ActionCd = "CANCEL_LABEL"
                    Return DoBaseExecute(task)

                    '途中送信
                Case EnumBarcodeType.SEND_CODE

                    Dim sendFlg As Boolean = CPersonal.ConfirmWindow(pdaTask, EnumWindowMode.YESNO_MODE, _
                                                 Color.Black, CConst.EnumSound.YESNO_BUZZ, CConst.CNS_MSG_026, Color.White)
                    If sendFlg Then
                        Dim filePath As String = CApplicationManager.GetRealPath(CApplicationManager.SETTING_PATH, CApplicationManager.EnumSpecialFolder.PATH_MYDOCUMENT)
                        CBgControl.BgThread("ODR", "csv", filePath, checkInfo.GetOrderData(pdaTask.PdaNo, pdaTask.EmpCd, "USE"), CConst.ORDER_DATA_UPLOAD)
                        pdaTask.WindowManager.SetMessage(CConst.CNS_MSG_027, CConst.COLOR_BLACK, CConst.COLOR_GREEN)

                    End If
                    task.ActionCd = "LABEL"
                    task.ProcessText = ""
                    Return GetNextState(task)

                Case EnumBarcodeType.KEY_CANCEL

                    task.ProcessText = ""
                    task.ActionCd = "CANCEL_LABEL"
                    Return DoBaseExecute(task)

                Case Else

                    'MessageBox.Show("task.ProcessText::::" & task.ProcessText)
                    '社内ラベル以外はエラー
                    CPersonal.ErrorWindow(pdaTask, EnumWindowMode.ALERT_MODE, _
                                       Color.Black, CConst.EnumSound.ALERT_BUZZ, CConst.CNS_MSG_007, Color.White)
                    task.ProcessText = ""
                    task.ActionCd = "CANCEL_LABEL"
                    Return DoBaseExecute(task)

            End Select

        Catch ex As Exception
            'MessageBox.Show("CCANCEL_LABEL_ERROR")

        End Try

        task.ProcessText = ""
        task.ActionCd = "LABEL"
        nextState = GetNextState(task)
        Return nextState
    End Function

    ''' <summary>
    ''' 終了メソッド(未使用)
    ''' </summary>
    ''' <param name="task"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Overrides Function DoExit(ByVal task As CTaskBase) As Boolean
        Debug.WriteLine("CCANCELLabelState.DoExit")
        Return True
    End Function


End Class
